home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news!enno
- From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
- Newsgroups: comp.lang.c++
- Subject: Re: Problems with inheritance and templates in g++
- Date: 08 Jan 1996 18:32:06 GMT
- Organization: Fachbereich Informatik, TH Darmstadt
- Distribution: world
- Message-ID: <ENNO.96Jan8193206@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- References: <4cre8f$bkp@magus.cs.utah.edu>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- In-reply-to: bendixen@eng.utah.edu's message of 8 Jan 1996 15:46:23 GMT
-
- In article <4cre8f$bkp@magus.cs.utah.edu> bendixen@eng.utah.edu (Mason Bendixen) writes:
-
- Hi,
- During the link stage, g++ complains about
- undefined symbols that correspond to unused member
- functions of instantiated objects. The code in question
- compiles fine under Borland C++ 4.5 but not g++.
- Take the sample file:
-
- // BEGIN SAMPLE PROGRAM
-
- #include <iostream.h>
-
- template <class T>
- class base {
- public:
- base () {}
- virtual int a() = 0;
- virtual int b() = 0;
- static base<T>* make();
- };
-
- template <class T>
- class test : public base<T> {
- public:
- test() {}
- int a();
- int b();
- };
-
- template <class T>
- base<T>* base<T>::make() {return new test<T>;}
-
- template <class T>
- int test<T>::a() {return 1;}
-
- template <class T>
- int test<T>::b() {return 2;}
-
- int main() {
- test<int> b;
- //int i = b.a();
- //int j = b.b();
- base<int>* t = base<int>::make();
- cout << t->a() << endl;
- return 0;
- }
-
- // END SAMPLE PROGRAM
-
-
- When I try to compile I get:
-
- >> 97 cadesm48% g++ -o tester temp.C
- >> collect2: ld returned 2 exit status
- >> ld: Undefined symbol
- >> test<int>::a(void)
- >> test<int>::b(void)
-
- If I uncomment the following lines:
-
- >> int i = b.a();
- >> int j = b.b();
-
- The program will work just fine.
- Can I force g++ to instantiate all member of every
- instantiated object? I think this has something to
- do with the way g++ handles inheritance for templatized
- classes. Any suggestions, insight, or possible work
- a rounds in this age of pre- standardized C++ would be
- greatly appreciated.
-
- I think you are using an obsolete version of g++.
- Anyway you can force the implementation of any member-function of a generic
- class by using explicit template instantiation. For example to instantiate
- test<int> and all its member-functions use
-
- template class test<int>;
-
- Enno
-